<?php
include_once 'MySQLDAO.php';

/**
 * Definition of Class Customers
 * This class represents a data access object for the database table 'customers'.
 */
class Customers {


	public $dao = null;

	private $i = 1;

	/**
	 * associated database field: customerId, type: int, is nullable: false
	 */
	private $customerId  = 0;

	/**
	 * associated database field: customerTitle, type: varchar, is nullable: true, character set: utf8, max length: 10
	 */
	private $customerTitle;

	/**
	 * associated database field: customerLastName, type: varchar, is nullable: true, character set: utf8, max length: 45
	 */
	private $customerLastName;

	/**
	 * associated database field: customerFirstName, type: varchar, is nullable: true, character set: utf8, max length: 45
	 */
	private $customerFirstName;

	/**
	 * associated database field: customerDOB, type: date, is nullable: true
	 */
	private $customerDOB;

	/**
	 * associated database field: customerHomePhone, type: varchar, is nullable: true, character set: utf8, max length: 20
	 */
	private $customerHomePhone;

	/**
	 * associated database field: addressId, type: int, is nullable: false
	 */
	private $addressId;

	/**
	 * associated database field: customerMobilePhone, type: varchar, is nullable: true, character set: utf8, max length: 20
	 */
	private $customerMobilePhone;

	/**
	 * associated database field: customerEmail, type: varchar, is nullable: true, character set: utf8, max length: 45
	 */
	private $customerEmail;

	/**
	 * associated database field: password, type: varchar, is nullable: true, character set: utf8, max length: 45
	 */
	private $password;

	private $bookingsArray = null;

	private $paymentsArray = null;

	private $address = null;



	/**
	 * Default constructor
	 * @param value some value
	 */
	function __construct(){
		$this->dao = new MySQLDao("Customers");
	}


	/**
	 * Initialize the business object with data read from the DB.
	 * @param row database row array containing the values from one read record
	 */
	public function init($row){
		if(isset($row['customerId'])) $this->customerId = $row['customerId'];
		if(isset($row['customerTitle'])) $this->customerTitle = $row['customerTitle'];
		if(isset($row['customerLastName'])) $this->customerLastName = $row['customerLastName'];
		if(isset($row['customerFirstName'])) $this->customerFirstName = $row['customerFirstName'];
		if(isset($row['customerDOB'])) $this->customerDOB = $row['customerDOB'];
		if(isset($row['customerHomePhone'])) $this->customerHomePhone = $row['customerHomePhone'];
		if(isset($row['addressId'])) $this->addressId = $row['addressId'];
		if(isset($row['customerMobilePhone'])) $this->customerMobilePhone = $row['customerMobilePhone'];
		if(isset($row['customerEmail'])) $this->customerEmail = $row['customerEmail'];
		if(isset($row['password'])) $this->password = $row['password'];
	}


	/**
	 * Returns the string representation for this object
	 * @param escapeQuotationMarks boolean value to indicate if quotation marks in value strings should be escaped, default value is true
	 * @return String String representation of Customers
	 */
	public function toString($escapeQuotationMarks=true){
		$s = '[';
		$s .= 'customerId: '.(($escapeQuotationMarks===true) ? addslashes($this->customerId) : $this->customerId).', ';
		$s .= 'customerTitle: '.(($escapeQuotationMarks===true) ? addslashes($this->customerTitle) : $this->customerTitle).', ';
		$s .= 'customerLastName: '.(($escapeQuotationMarks===true) ? addslashes($this->customerLastName) : $this->customerLastName).', ';
		$s .= 'customerFirstName: '.(($escapeQuotationMarks===true) ? addslashes($this->customerFirstName) : $this->customerFirstName).', ';
		$s .= 'customerDOB: '.(($escapeQuotationMarks===true) ? addslashes($this->customerDOB) : $this->customerDOB).', ';
		$s .= 'customerHomePhone: '.(($escapeQuotationMarks===true) ? addslashes($this->customerHomePhone) : $this->customerHomePhone).', ';
		$s .= 'addressId: '.(($escapeQuotationMarks===true) ? addslashes($this->addressId) : $this->addressId).', ';
		$s .= 'customerMobilePhone: '.(($escapeQuotationMarks===true) ? addslashes($this->customerMobilePhone) : $this->customerMobilePhone).', ';
		$s .= 'customerEmail: '.(($escapeQuotationMarks===true) ? addslashes($this->customerEmail) : $this->customerEmail).', ';
		$s .= 'password: '.(($escapeQuotationMarks===true) ? addslashes($this->password) : $this->password);
		$s .= ']';
		return $s;
	}


	/**
	 * Returns the JSON (JavaScript Object Notation) string representation for this object
	 * @return String JSON representation of Customers
	 */
	public function toJSONString(){
		$fieldValues = array ("customerId"=>$this->customerId, "customerTitle"=>$this->customerTitle, "customerLastName"=>$this->customerLastName, "customerFirstName"=>$this->customerFirstName, "customerDOB"=>$this->customerDOB, "customerHomePhone"=>$this->customerHomePhone, "addressId"=>$this->addressId, "customerMobilePhone"=>$this->customerMobilePhone, "customerEmail"=>$this->customerEmail, "password"=>$this->password);
		return json_encode($fieldValues);
	}


	/**
	 * Returns a HTML representation of this object using DIVs for formatting via css
	 * @param escapeHTML boolean value to indicate if value strings should be html escaped, default value is true
	 * @return String the HTML representation of the Customers object
	 */
	public function toHTMLString($escapeHTML=true){
		$htmlCode = "<div class=\"customers\">\n";
		$htmlCode .= '	<div class="customers_customerId">'.(($escapeHTML===true) ? htmlspecialchars($this->customerId) : $this->customerId)."</div>\n";
		$htmlCode .= '	<div class="customers_customerTitle">'.(($escapeHTML===true) ? htmlspecialchars($this->customerTitle) : $this->customerTitle)."</div>\n";
		$htmlCode .= '	<div class="customers_customerLastName">'.(($escapeHTML===true) ? htmlspecialchars($this->customerLastName) : $this->customerLastName)."</div>\n";
		$htmlCode .= '	<div class="customers_customerFirstName">'.(($escapeHTML===true) ? htmlspecialchars($this->customerFirstName) : $this->customerFirstName)."</div>\n";
		$htmlCode .= '	<div class="customers_customerDOB">'.(($escapeHTML===true) ? htmlspecialchars($this->customerDOB) : $this->customerDOB)."</div>\n";
		$htmlCode .= '	<div class="customers_customerHomePhone">'.(($escapeHTML===true) ? htmlspecialchars($this->customerHomePhone) : $this->customerHomePhone)."</div>\n";
		$htmlCode .= '	<div class="customers_addressId">'.(($escapeHTML===true) ? htmlspecialchars($this->addressId) : $this->addressId)."</div>\n";
		$htmlCode .= '	<div class="customers_customerMobilePhone">'.(($escapeHTML===true) ? htmlspecialchars($this->customerMobilePhone) : $this->customerMobilePhone)."</div>\n";
		$htmlCode .= '	<div class="customers_customerEmail">'.(($escapeHTML===true) ? htmlspecialchars($this->customerEmail) : $this->customerEmail)."</div>\n";
		$htmlCode .= '	<div class="customers_password">'.(($escapeHTML===true) ? htmlspecialchars($this->password) : $this->password)."</div>\n";
		$htmlCode .= "</div>\n";
		return $htmlCode;
	}
	/*
	public function getData($where) {
		$rows = $this->dao->getData($where);
		return $rows;
	}*/


	public static function loadAll(){
		$customers = new Customers();
		$rows = $customers->dao->getData("");
		$customerss = array();
		for ($index = 0; $index < sizeof($rows); $index++) {
			$d = new Customers();
			$d->init($rows[$index]);
			array_push($customerss,$d);
		}
		return $customerss;
	}


	/**
	 * Load the Customers uniquely by its primary key.
	 * @param customerId the primary key
	 * @return Customers Instance of {@link Customers}
	 */
	public static function loadUnique($customerId){
		$customers = new Customers();
		$rows = $customers->dao->getData("`customerId`='$customerId'");
		$customers->init($rows[0]);
		return $customers;
	}
	
	/**
	 * Load the Customer that has the email address.
	 * @param emailAddress
	 * @return Customers instance of {@link Customers}
	 */
	 public static function loadByCustomerEmail($customerEmail) {
		 $customers = new Customers();
		 $rows = $customers->dao->getData("`customerEmail`='$customerEmail'");
		 $customers->init($rows[0]);
		 return $customers;
	 }


	/**
	 * Load all records of Customers uniquely by its foreign keys:
	 * addressId
	 * @param addressId foreign key
	 * @return array of Instance of {@link Customers}
	 */
	public static function loadByFKAddressid($addressId){
		$customers = new Customers();
		$rows = $customers->dao->getData("`addressId`='$addressId'");
		$customerss = array();
		for ($index = 0; $index < sizeof($rows); $index++) {
			$d = new Customers();
			$d->init($rows[$index]);
			array_push($customerss,$d);
		}
		return $customerss;
	}


	/**
	 * Insert this object into the DB
	 * @return number new id (auto increment value) generated
	 */
	public function insert(){
		//$list = array("customerId"=>$this->customerId, "customerTitle"=>$this->customerTitle, "customerLastName"=>$this->customerLastName, "customerFirstName"=>$this->customerFirstName, "customerDOB"=>$this->customerDOB, "customerHomePhone"=>$this->customerHomePhone, "addressId"=>$this->addressId, "customerMobilePhone"=>$this->customerMobilePhone, "customerEmail"=>$this->customerEmail, "password"=>$this->password);
		$formatCustomerDOB = date('Y-m-d', strtotime($this->customerDOB));
		$list = array("customerTitle"=>$this->customerTitle, "customerLastName"=>$this->customerLastName, "customerFirstName"=>$this->customerFirstName, "customerDOB"=>$formatCustomerDOB, "customerHomePhone"=>$this->customerHomePhone, "addressId"=>$this->addressId, "customerMobilePhone"=>$this->customerMobilePhone, "customerEmail"=>$this->customerEmail, "password"=>$this->password);
		return $this->dao->insertRecord($list);
	}


	/**
	 * Update this object into the DB
	 * @return number the number of updated records
	 */
	public function update(){
		//$list = array("customerId"=>$this->customerId, "customerTitle"=>$this->customerTitle, "customerLastName"=>$this->customerLastName, "customerFirstName"=>$this->customerFirstName, "customerDOB"=>$this->customerDOB, "customerHomePhone"=>$this->customerHomePhone, "addressId"=>$this->addressId, "customerMobilePhone"=>$this->customerMobilePhone, "customerEmail"=>$this->customerEmail, "password"=>$this->password);
		$formatCustomerDOB = date('Y-m-d', strtotime($this->customerDOB));
		$list = array("customerTitle"=>$this->customerTitle, "customerLastName"=>$this->customerLastName, "customerFirstName"=>$this->customerFirstName, "customerDOB"=>$formatCustomerDOB, "customerHomePhone"=>$this->customerHomePhone, "addressId"=>$this->addressId, "customerMobilePhone"=>$this->customerMobilePhone, "customerEmail"=>$this->customerEmail, "password"=>$this->password);
		$where = "`customerId`='$this->customerId'";
		return $this->dao->updateRecord($list,$where);
	}


	/**
	 * Initialize the business object with data read from the DB.
	 */
	public function initPOST(){
		if (isset($_POST['customerId'])){
			$this->customerId = $_POST['customerId'];
		}
		if (isset($_POST['Title'])){
			$this->customerTitle = $_POST['Title'];
		}
		if (isset($_POST['LastName'])){
			$this->customerLastName = $_POST['LastName'];
		}
		if (isset($_POST['FirstName'])){
			$this->customerFirstName = $_POST['FirstName'];
		}
		if (isset($_POST['DOB'])){
			$this->customerDOB = $_POST['DOB'];
		}
		if (isset($_POST['HomePhone'])){
			$this->customerHomePhone = $_POST['HomePhone'];
		}
		if (isset($_POST['addressId'])){
			$this->addressId = $_POST['addressId'];
		}
		if (isset($_POST['MobilePhone'])){
			$this->customerMobilePhone = $_POST['MobilePhone'];
		}
		if (isset($_POST['EmailAddress'])){
			$this->customerEmail = $_POST['EmailAddress'];
		}
		if (isset($_POST['Password'])){
			$this->password = $_POST['Password'];
		}
	}


	/**
	 * Initialize the business object with data read from the DB.
	 */
	public function initGET(){
		if (isset($_GET['customerId'])){
			$this->customerId = $_GET['customerId'];
		}
		if (isset($_GET['Title'])){
			$this->customerTitle = $_GET['Title'];
		}
		if (isset($_GET['LastName'])){
			$this->customerLastName = $_GET['LastName'];
		}
		if (isset($_GET['FirstName'])){
			$this->customerFirstName = $_GET['FirstName'];
		}
		if (isset($_GET['DOB'])){
			$this->customerDOB = $_GET['DOB'];
		}
		if (isset($_GET['HomePhone'])){
			$this->customerHomePhone = $_GET['HomePhone'];
		}
		if (isset($_GET['addressId'])){
			$this->addressId = $_GET['addressId'];
		}
		if (isset($_GET['MobilePhone'])){
			$this->customerMobilePhone = $_GET['MobilePhone'];
		}
		if (isset($_GET['EmailAddress'])){
			$this->customerEmail = $_GET['EmailAddress'];
		}
		if (isset($_GET['Password'])){
			$this->password = $_GET['Password'];
		}
	}


	/**
	 * Returns the value from the database field customerId.
	 * In the database the field customerId is of type int.
	 * The database field is NOT nullable.
	 * @return String the value of the field
	 */
	public function getCustomerId(){
		return $this->customerId;
	}


	/**
	 * Set the value from the database field customerId.
	 * In the database the field customerId is of type int.
	 * The database field is NOT nullable.
	 * @param customerId String with the value for the field to be set
	 */
	public function setCustomerId($customerId){
		$this->customerId = $customerId;
	}


	/**
	 * Returns the value from the database field customerTitle.
	 * In the database the field customerTitle is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getCustomerTitle(){
		return $this->customerTitle;
	}


	/**
	 * Set the value from the database field customerTitle.
	 * In the database the field customerTitle is of type varchar.
	 * The database field is  nullable.
	 * @param customerTitle String with the value for the field to be set
	 */
	public function setCustomerTitle($customerTitle){
		if(strlen($customerTitle) > 10) throw new Exception("customerTitle does not allow strings longer than 10 characters. The submitted string contains ".strlen($customerTitle)." characters.");
		$this->customerTitle = $customerTitle;
	}


	/**
	 * Returns the value from the database field customerLastName.
	 * In the database the field customerLastName is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getCustomerLastName(){
		return $this->customerLastName;
	}


	/**
	 * Set the value from the database field customerLastName.
	 * In the database the field customerLastName is of type varchar.
	 * The database field is  nullable.
	 * @param customerLastName String with the value for the field to be set
	 */
	public function setCustomerLastName($customerLastName){
		if(strlen($customerLastName) > 45) throw new Exception("customerLastName does not allow strings longer than 45 characters. The submitted string contains ".strlen($customerLastName)." characters.");
		$this->customerLastName = $customerLastName;
	}


	/**
	 * Returns the value from the database field customerFirstName.
	 * In the database the field customerFirstName is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getCustomerFirstName(){
		return $this->customerFirstName;
	}


	/**
	 * Set the value from the database field customerFirstName.
	 * In the database the field customerFirstName is of type varchar.
	 * The database field is  nullable.
	 * @param customerFirstName String with the value for the field to be set
	 */
	public function setCustomerFirstName($customerFirstName){
		if(strlen($customerFirstName) > 45) throw new Exception("customerFirstName does not allow strings longer than 45 characters. The submitted string contains ".strlen($customerFirstName)." characters.");
		$this->customerFirstName = $customerFirstName;
	}


	/**
	 * Returns the value from the database field customerDOB.
	 * In the database the field customerDOB is of type date.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getCustomerDOB(){
		return $this->customerDOB;
	}


	/**
	 * Set the value from the database field customerDOB.
	 * In the database the field customerDOB is of type date.
	 * The database field is  nullable.
	 * @param customerDOB String with the value for the field to be set
	 */
	public function setCustomerDOB($customerDOB){
		$this->customerDOB = $customerDOB;
	}


	/**
	 * Returns the value from the database field customerHomePhone.
	 * In the database the field customerHomePhone is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getCustomerHomePhone(){
		return $this->customerHomePhone;
	}


	/**
	 * Set the value from the database field customerHomePhone.
	 * In the database the field customerHomePhone is of type varchar.
	 * The database field is  nullable.
	 * @param customerHomePhone String with the value for the field to be set
	 */
	public function setCustomerHomePhone($customerHomePhone){
		if(strlen($customerHomePhone) > 20) throw new Exception("customerHomePhone does not allow strings longer than 20 characters. The submitted string contains ".strlen($customerHomePhone)." characters.");
		$this->customerHomePhone = $customerHomePhone;
	}


	/**
	 * Returns the value from the database field addressId.
	 * In the database the field addressId is of type int.
	 * The database field is NOT nullable.
	 * @return String the value of the field
	 */
	public function getAddressId(){
		return $this->addressId;
	}


	/**
	 * Set the value from the database field addressId.
	 * In the database the field addressId is of type int.
	 * The database field is NOT nullable.
	 * @param addressId String with the value for the field to be set
	 */
	public function setAddressId($addressId){
		$this->addressId = $addressId;
	}


	/**
	 * Returns the value from the database field customerMobilePhone.
	 * In the database the field customerMobilePhone is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getCustomerMobilePhone(){
		return $this->customerMobilePhone;
	}


	/**
	 * Set the value from the database field customerMobilePhone.
	 * In the database the field customerMobilePhone is of type varchar.
	 * The database field is  nullable.
	 * @param customerMobilePhone String with the value for the field to be set
	 */
	public function setCustomerMobilePhone($customerMobilePhone){
		if(strlen($customerMobilePhone) > 20) throw new Exception("customerMobilePhone does not allow strings longer than 20 characters. The submitted string contains ".strlen($customerMobilePhone)." characters.");
		$this->customerMobilePhone = $customerMobilePhone;
	}


	/**
	 * Returns the value from the database field customerEmail.
	 * In the database the field customerEmail is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getCustomerEmail(){
		return $this->customerEmail;
	}


	/**
	 * Set the value from the database field customerEmail.
	 * In the database the field customerEmail is of type varchar.
	 * The database field is  nullable.
	 * @param customerEmail String with the value for the field to be set
	 */
	public function setCustomerEmail($customerEmail){
		if(strlen($customerEmail) > 45) throw new Exception("customerEmail does not allow strings longer than 45 characters. The submitted string contains ".strlen($customerEmail)." characters.");
		$this->customerEmail = $customerEmail;
	}


	/**
	 * Returns the value from the database field password.
	 * In the database the field password is of type varchar.
	 * The database field is  nullable.
	 * @return String the value of the field
	 */
	public function getPassword(){
		return $this->password;
	}


	/**
	 * Set the value from the database field password.
	 * In the database the field password is of type varchar.
	 * The database field is  nullable.
	 * @param password String with the value for the field to be set
	 */
	public function setPassword($password){
		if(strlen($password) > 45) throw new Exception("password does not allow strings longer than 45 characters. The submitted string contains ".strlen($password)." characters.");
		$this->password = $password;
	}


	/**
	 * Returns an object array containing all linked Bookings objects.
	 * A One to Many(1:n) relation exists between the Customers and the Bookings.
	 * @return Bookings[] Array of Bookings instances
	 */
	public function getBookingss(){
	if($this->bookingsArray == null){
			$this->bookingsArray = array();
			$tempbookings = new Bookings();
			$dataRowsTemp = $tempbookings->dao->getData("`customerId` = '$this->customerId'");
			if(count($dataRowsTemp)  > 0)
			foreach($dataRowsTemp as $dataRow){
				$tempBookings = new Bookings();
				$tempBookings->init($dataRow);
				array_push($this->bookingsArray , $tempBookings);
			}
		}
		return $this->bookingsArray;
	}


	/**
	 * Returns an object array containing all linked Payments objects.
	 * A One to Many(1:n) relation exists between the Customers and the Payments.
	 * @return Payments[] Array of Payments instances
	 */
	public function getPaymentss(){
	if($this->paymentsArray == null){
			$this->paymentsArray = array();
			$temppayments = new Payments();
			$dataRowsTemp = $temppayments->dao->getData("`customerId` = '$this->customerId'");
			if(count($dataRowsTemp)  > 0)
			foreach($dataRowsTemp as $dataRow){
				$tempPayments = new Payments();
				$tempPayments->init($dataRow);
				array_push($this->paymentsArray , $tempPayments);
			}
		}
		return $this->paymentsArray;
	}


	/**
	 * Set links to related Bookings instances. Existing links will be overwritten
	 * @param bookingsArray array of Bookings instances to be linked to this object instance
	 */
	public function setBookingss($bookingsArray){
		foreach($bookingsArray as $bookings)
			$bookings->customerId = $this->customerId;
		$this->bookingsArray = $bookingsArray;
	}


	/**
	 * Add a link to object of type Bookings
	 * @param bookings instance of Bookings to be linked to this object instance
	 */
	public function addBookings($bookings){
		$bookings->customerId = $this->customerId;
		if($this->bookingsArray == null) $this->bookingsArray = array();
		array_push($this->bookingsArray, $bookings);
	}


	/**
	 * Set links to related Payments instances. Existing links will be overwritten
	 * @param paymentsArray array of Payments instances to be linked to this object instance
	 */
	public function setPaymentss($paymentsArray){
		foreach($paymentsArray as $payments)
			$payments->customerId = $this->customerId;
		$this->paymentsArray = $paymentsArray;
	}


	/**
	 * Add a link to object of type Payments
	 * @param payments instance of Payments to be linked to this object instance
	 */
	public function addPayments($payments){
		$payments->customerId = $this->customerId;
		if($this->paymentsArray == null) $this->paymentsArray = array();
		array_push($this->paymentsArray, $payments);
	}


	/**
	 * Returns an instance of the linked Address object.
	 * A One to One(1:1) relation exists between the Address and the Customers.
	 * @return Address the linked instance of Address
	 */
	public function getAddress(){
		if($this->address == null){
			$this->address = Address::loadUnique($this->addressId);
		}
		return $this->address;
	}


	/**
	 * Set a link to a related Address instance
	 * @param address instance of Address to be linked to this object instance
	 */
	public function setAddress($address){
		$address->addressId = $this->addressId;
		$this->address = $address;
	}
}

?>